package com.enqualcomm.kids.activities;

import android.net.Uri;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.mvp.NetworkModel;
import com.enqualcomm.kids.network.NetworkListener;
import com.enqualcomm.kids.network.SocketRequest;
import com.enqualcomm.kids.network.socket.request.QueryChannelConfigParams;
import com.enqualcomm.kids.network.socket.response.QueryChannelConfigResult;
import com.facebook.drawee.view.SimpleDraweeView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

/**
 * Created by Sunzhigang on 2016/8/29.
 */
@EActivity(R.layout.activity_wx_ggh)
public class WXGGDisplayActivity extends BaseActivity {

    @ViewById(R.id.scan_qr_notice_tv)
    TextView scan_qr_notice_tv;
    @ViewById(R.id.qr_image_view)
    SimpleDraweeView qr_image_view;
    NetworkModel networkModel;
    @ViewById(R.id.title_bar_title_tv)
    TextView title_bar_title_tv;

    Uri uri;
    AppDefault appDefault;

    @AfterViews
    void setupViews() {
        title_bar_title_tv.setText(getString(R.string.wx_gg_title_tv));
        TextView title_bar_terminal_name_tv = (TextView) findViewById(R.id.title_bar_terminal_name_tv);
        View title_bar_terminal_icon_iv = findViewById(R.id.title_bar_terminal_icon_iv);
        title_bar_terminal_icon_iv.setVisibility(View.GONE);
        title_bar_terminal_name_tv.setVisibility(View.VISIBLE);
        title_bar_terminal_name_tv.setText(getString(R.string.wx_gg_title_tv));
        appDefault = new AppDefault();
        if (appDefault.getChannelConfig() != null) {
            String url = appDefault.getChannelConfig().twocodepic;
            if (!TextUtils.isEmpty(url)) {
                uri = Uri.parse(url);
                qr_image_view.setImageURI(uri);
            }
        }


        QueryChannelConfigParams params = new QueryChannelConfigParams();
        networkModel = new NetworkModel();
        networkModel.loadDataFromServer(new SocketRequest<QueryChannelConfigResult>(params, new NetworkListener<QueryChannelConfigResult>() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(QueryChannelConfigResult response) {
                if (response.code == 0) {
                    new AppDefault().setChannelConfig(response);
                    if (!TextUtils.isEmpty(response.result.twocodepic)) {
                        uri = Uri.parse(response.result.twocodepic);
                        qr_image_view.setImageURI(uri);
                    }
                }
            }
        }));

        String text = getString(R.string.wx_gg_decribe_tv_1) + "<font color=\"#86c610\">" + getString(R.string.wx_gg_decribe_tv_2) + "</font>" + getString(R.string.wx_gg_decribe_tv_4);
        scan_qr_notice_tv.setText(Html.fromHtml(text));
    }

    @Click(R.id.title_bar_left_iv)
    void goFinish() {
        finish();
    }


}
